package com.atos.smp.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.atos.smp.beans.Collaborator;
import com.atos.smp.beans.Project;
import com.atos.smp.beans.Rule;
import com.atos.smp.dao.ProjectDao;

public class ProjectDaoImpl extends HibernateDaoSupport implements ProjectDao,
		Serializable {

	
	public void save(Project project) {
		getHibernateTemplate().persist(project);
	}

	
	public void update(Project p) {
		Project p2 = findProjectById(p.getId());
		p2.setCollaborators(p.getCollaborators());
		getHibernateTemplate().update(p2);
	}

	public void updateCollaborators(Project p) {
		Project p2 = findProjectById(p.getId());
		p2.setCollaborators(p.getCollaborators());
		getHibernateTemplate().update(p2);
	}

	
	
	public void delete(Project project) {
		getHibernateTemplate().delete(project);
	}


	public Project findProjectById(int id) {
		List<Project> list = getHibernateTemplate().find(
				"from Project where id = " + id);
		if (list != null && list.size() != 0)
			return (Project) list.get(0);
		return null;
	}

	
	public List<Project> findAll() {
		List<Project> list = getHibernateTemplate().find("from Project");
		for (Project project : list) {
			project.getTools().size();
		}

		return list;
	}

	@Override
	public List<Collaborator> findAllManagers() {
		@SuppressWarnings("unchecked")
		List<Collaborator> managers = getHibernateTemplate().find(
				"from Collaborator where rule='" + Rule.Manager + "'");
		return managers;
	}

	@Override
	public List<Collaborator> findAllProjectManagers() {
		@SuppressWarnings("unchecked")
		List<Collaborator> pmanagers = getHibernateTemplate().find(
				"from Collaborator where rule='" + Rule.Project_Manager + "'");
		return pmanagers;
	}

	
	@Override
	public List<Collaborator> findAllCollaborators() {
		@SuppressWarnings("unchecked")
		List<Collaborator> colabs = getHibernateTemplate().find(
				"from Collaborator where rule='" + Rule.Collaborator + "'");
		return colabs;
	}

	@Override
	public List<Collaborator> findAllCollaboratorsNotInProject(int id) {
		
		 Project p = findProjectById(id);
		 
		 Conjunction dis = Restrictions.conjunction();
			for (Collaborator r : p.getCollaborators()) {
			    Criterion expression = Restrictions.idEq(r.getDas());
				Criterion criterion = Restrictions.eq("rule", Rule.Collaborator);
				dis.add(Restrictions.not(expression)).add(criterion);
			    
			}		
		@SuppressWarnings("unchecked")
		List<Collaborator> collaborators = getHibernateTemplate()
				.findByCriteria(
						DetachedCriteria.forClass(Collaborator.class)
								.add(dis));
		return collaborators;
	}

	public List<Project> findProjectsByDas(String das) {
		@SuppressWarnings("unchecked")
		List<Project> list2 = getHibernateTemplate().findByCriteria(
				DetachedCriteria.forClass(Project.class)
						.createAlias("collaborators", "c")
						.add(Restrictions.eq("c.das", das)));
		return list2;
	}

}
